package com.lfz.quality_effect.utils;


import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * @author admin-11
 * @title: EasyExcelUtils
 * @projectName judicial
 * @description: TODO
 * @date 2019/7/115:57
 */
public class EasyExcelUtils {
    private static final String SERIAL_VERSION_UID = "serialVersionUID";

    /**
     * 判断对象中属性值是否全为空
     */
    public static boolean checkObjAllFieldsIsNull(Object object) {
        if (null == object) {
            return true;
        }
        try {
            for (Field f : object.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                //只校验带ExcelProperty注解的属性
                ExcelProperty property = f.getAnnotation(ExcelProperty.class);
                if(property == null || SERIAL_VERSION_UID.equals(f.getName())){
                    continue;
                }
                if (f.get(object) != null && StringUtils.isNotBlank(f.get(object).toString())) {
                    return false;
                }
            }
        } catch (Exception e) {
            //do something
        }
        return true;
    }
    public static boolean createExcelStreamMutilByEaysExcel(Map<String, List<? extends BaseRowModel>> SheetNameAndDateList, ExcelTypeEnum type, String filePath) throws UnsupportedEncodingException {
        if (checkParam(SheetNameAndDateList, type)) {
            return false;
        }

        try {
            OutputStream out = new FileOutputStream(filePath);
            //  OutputStream out = new FileOutputStream(filePath+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".xlsx");

            ExcelWriter writer = new ExcelWriter(out, type, true);

            setSheet(SheetNameAndDateList, writer,filePath,out);
            writer.finish();
            out.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * @Description 校验参数
     * @Date 上午12:39 2019/1/31
     */
    private static boolean checkParam(Map<String, List<? extends BaseRowModel>> SheetNameAndDateList, ExcelTypeEnum type) {
        if (MapUtils.isEmpty(SheetNameAndDateList)) {
            return true;
        } else if (type == null) {
            return true;
        }
        return false;
    }


    public static void setSheet(Map<String, List<? extends BaseRowModel>> SheetNameAndDateList, ExcelWriter writer,String filePath,OutputStream out) {
        int sheetNum = 1;
        //倒序输出sheet表顺序
        for (Map.Entry<String, List<? extends BaseRowModel>> stringListEntry : SheetNameAndDateList.entrySet()) {
            Sheet sheet = new Sheet(sheetNum, 0, stringListEntry.getValue().get(0).getClass());
            sheet.setAutoWidth(true);
            // sheet.setStartRow(8);
            writer.write(stringListEntry.getValue(), sheet);
            sheetNum++;

        }
    }
}
